import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'MyYSTextView.dart';



typedef MyTextViewCreatedCallback = void Function(MyYSTextView controller);

class MyTextView extends StatefulWidget {

  const MyTextView({
    Key key,
    this.onViewCreated,
  }) : super(key: key);

  final MyTextViewCreatedCallback onViewCreated;

  @override
  State<StatefulWidget> createState() => _MyTextViewState();
}

class _MyTextViewState extends State<MyTextView> {

  MyYSTextView _controller;

  MethodChannel _channel;

  StandardMessageCodec standardMessageCodec = const StandardMessageCodec();

  @override
  Widget build(BuildContext context) {
    print("构建新的样式");
    return AndroidView(
      viewType: 'woshiku.flutter.io/launchApp',
      creationParams: {
        "myContent": "通过参数传入的文本内容,I am 原生view",
      },
      creationParamsCodec: standardMessageCodec,
      onPlatformViewCreated: this.onMyViewCreated,
    );
  }


  void onMyViewCreated(int id) {

    String channelId = 'plugins.nightfarmer.top/myview_$id';

    _channel = new MethodChannel(channelId);

    // _controller = ControllerTextView.android( standardMessageCodec, this, channelId);
    _controller = MyYSTextView(channelId);

    widget.onViewCreated(_controller);
    _controller.onCreate();
    Future.delayed(Duration(milliseconds: 1000), () => 0)
        .then((value) {
          if(_controller == null){
            print("ControllerTextView 初始化失败");
          }else{
            _controller.onResume();
          }

      setMyViewText("重新设置传入的文本");
    });


  }

  Future<void> setMyViewText(String text) async {
    assert(text != null);
    return _channel.invokeMethod('setText', text);
  }


  @override
  void dispose() {
    _controller.onDestroy();
    super.dispose();
  }

}